const { UserModel } = require('../model/DataModel')

class UserService {
  create(data) {
    return UserModel.create({ ...data })
  }

  findByName(name) {
    return new Promise((resolve, reject) => {
      const reg = new RegExp(name, 'i')
      UserModel.find({ $or: [{ name: { $regex: reg } }] }, { password: 0 }, (err, res) => {
        if (err) {
          reject(err)
        } else {
          resolve(res)
        }
      })
    })
  }

  findByEmail(email) {
    return new Promise((resolve, reject) => {
      UserModel.findOne({ email }, (err, res) => {
        if (err) {
          reject(err)
        } else {
          resolve(res)
        }
      })
    })
  }

  async updateUser(userData) {
     return await UserModel.updateOne({...userData})
  }
}

module.exports = new UserService()